﻿using Dapper.DBContext;
using Guoc.BigMall.Domain.Entity;
using Guoc.BigMall.Web.Services;
using Guoc.BigMall.WebService;
using Guoc.BigMall.WebService.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Guoc.BigMall.Web.Controllers
{
    public class MemberController : Controller
    {
        IDBContext _db;
        IMemberService _memberService;
        public MemberController(IMemberService memberService, IDBContext context)
        {
            _db = context;
            this._memberService = memberService;
        }
        // GET: Member
        public ActionResult Orders(PageVo page,MyOrderSearch search)
        {
            var rows = _memberService.QueryMyOrders(page, search);
            return new JsonNetResult(ResultModel.Ok(rows));
        }

        /// <summary>
        ///  我的会员
        /// </summary>
        /// <param name="page"></param>
        /// <param name="memberId"></param>
        /// <returns></returns>
        public ActionResult My(PageVo page,int memberId) {
            var model = _memberService.QueryMyMembers(page, memberId);
            return new JsonNetResult(ResultModel.Ok(model));
        }

        public ActionResult MyDistribute(PageVo page, int? cashStatus,int memberId)
        {
            var rows = _memberService.QueryMyDistribute(page, cashStatus, memberId);
            return new JsonNetResult(ResultModel.Ok(rows));
        }

        public ActionResult Detail(int memberId)
        {
            var model = _db.Table<MemberVo>().FirstOrDefault(n => n.Id == memberId);
            if (model == null) {
                model = new MemberVo();
            }
            return new JsonNetResult(ResultModel.Ok(model));
        }

        public ActionResult Refer(int memberId)
        {
            var referMember= _db.Table<DistributeMember>().FirstOrDefault(n => n.MemberId == memberId);
            var  model = new MemberVo();
            if (referMember == null) {
                return new JsonNetResult(ResultModel.Ok(model));
            }
            return Detail(referMember.ParentMemberId);
        }

        public ActionResult CashToAlipay(AlipayCashDto model)
        {
            model.WebRootPath = Server.MapPath("~/");
            _memberService.CashToAlipay(model);
            return new JsonNetResult(ResultModel.Ok());
        }

        public ActionResult CashToWechat(WechatCashDto model)
        {
            model.WebRootPath = Server.MapPath("~/");
            model.IpAddress = Request.UserHostAddress;
            _memberService.CashToWechat(model);
            return new JsonNetResult(ResultModel.Ok());
        }
    }
}